<?php

class Security {

	private $identities = array();
	
	private static $identities_file = "lib/identities";
	private $enc_key = "aodkdfl1003";
	
	private static $instance;
	
	public function __construct(){
		$this->_loadIdentities();
	}
	
	private static function _getInstance(){
		if(!self::$instance){
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function _loadIdentities(){
		$data = file_get_contents(self::$identities_file);
		preg_match_all('/([^:;]+):([^;\r\n]+)/s', $data, $matches);
		foreach($matches[1] as $k=>$username){
			$this->identities[$username] = $matches[2][$k];
		}
	}

	public static function login($username, $password){
		$s = self::_getInstance();
		if($s->identities[$username] == crypt($password, $s->enc_key)){
			$_SESSION["user"] = $username;
		}
		redirect("/admin");
	}
	public static function logout(){
		unset($_SESSION["user"]);
	}

}

?>